package com.issac.vhr.controller.system.basic;

import com.issac.vhr.entity.Department;
import com.issac.vhr.entity.RespBean;
import com.issac.vhr.service.DepartmentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/dep/")
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
    public List<Department> getAllDepartmentByParentId(){
        //遍历部门层级 用递归查询 从根目录id -1 开始
        return departmentService.getAllDepartmentByParentId(-1);
    }
    @PostMapping("/")
    public RespBean addDepartment(@RequestBody Department department){
        /*
         * 1.添加
         * 2.查询parentId的DepPath + 自己的id组成自己的depPath
         * 3.查询父部门isParent t/f t不变 f改t
         *
         * */
        if (departmentService.addDepartment(department)){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteDepartment(@PathVariable Integer id){
        if (departmentService.deleteDepartment(id)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
